package com.corpdata.app.test.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Map;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.corpdata.common.utils.HttpUtils;

public class Main {//

	public static void main(String[] args) throws Exception {
		String token = "e5e1f9d4b06d4492a19734d808cf3e6d1533609382026";
		//getApi("3c.visitor.invite", "10000002", "", token);//访客二维码
		getApi("3c.door.queryvisitordoorinout", "10000002", "", token);//出入记录
	}

	private static void getApi(String serviceId, String personCode, String cardId, String token)
			throws NoSuchAlgorithmException, UnsupportedEncodingException, IOException, ClientProtocolException {
		// 接口地址
		String url = "http://www.jslife.com.cn/jsaims/as";
		// 客户号
		String cid = "880001001007616";
		// 接口版本号
		String v = "2";
		// 参数
		/*String p = "{\"serviceId\":\"" + serviceId+ "\",\"requestType\":\"DATA\","
				+ "\"attributes\":{\"areaCode\":\"0000007525\",\"personCode\":\""+ personCode+ "\",\"visitorType\":0,\"businesserCode\":\"54\",\"parkCode\":\"0000007525\","
				+ "\"timeDesc\":{\"sd\":\"2018-08-03 16:00:00\",\"ed\":\"2018-08-03 19:00:00\",\"st\":\"07:00\",\"et\":\"19:00\",\"w\":\"1111111\"}},"
				+ "\"dataItems\":[{\"attributes\":{\"visitorName\":\"123\",\"visitorTel\":\"13888888888\"}}]}";*/
		
		String p = "{ " + 
				"\"serviceId\":\""+ serviceId+"\", " + 
				"\"requestType\":\"DATA\", " + 
				"\"attributes\":{ " + 
				"	 \"areaCode\": \"0000007525\", " + 
				/*"\"visitorName\": \"ac1\", " + 
				"\"visitorTel\": \"13x12345678\", " + */
				"\"beginTime\": \"2018-08-01 00:00:00\", " + 
				"\"endTime\": \"2018-08-30 00:00:00\", " + 
				"\"pageSize\": 100, " + 
				"\"pageIndex\": 1 " + 
				" " + 
				"} " + 
				"} ";

		// 签名signKey
		String signKey = "2bb447142776d94fd7b9b51e5e9622d2";
		// 生成MD5签名
		MessageDigest md5Tool = MessageDigest.getInstance("MD5");
		byte[] md5Data = md5Tool.digest((p + signKey).getBytes("UTF-8"));
		String sn = toHexString(md5Data);

		// 构造参数
		CloseableHttpClient client = HttpClients.createDefault();
		HttpPost post = new HttpPost(url);
		ArrayList<NameValuePair> list = new ArrayList<NameValuePair>();
		list.add(new BasicNameValuePair("cid", cid));
		list.add(new BasicNameValuePair("v", v));
		list.add(new BasicNameValuePair("p", p));
		list.add(new BasicNameValuePair("sn", sn));
		list.add(new BasicNameValuePair("tn", token));
		HttpEntity en = new UrlEncodedFormEntity(list, Consts.UTF_8);
		post.setEntity(en);

		// 发送消息和处理结果
		CloseableHttpResponse response = client.execute(post);
		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			System.out.println("执行成功！");
			String s = EntityUtils.toString(response.getEntity());
			System.out.println(s);
		} else {
			System.out.println("执行失败！");
		}
		response.close();
	}

	private static String toHexString(byte[] bytes) {
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < bytes.length; i++) {
			buffer.append(String.format("%02X", bytes[i]));
		}
		return buffer.toString();
	}

}
